<template>
    <div>
        <Tables 
            :title ='title'
            :butns = 'butns' 
            :field = 'field' 
            :tableData ='tableData' 
            :perNum='per_num' 
            :totalNum="total_num" 
            :collectField = "collect_field"
            :curPage ="cur_page" 
            :pageSize='pageSize' 
            :clearSelectItems="selectedItems"
            :rankable ="true"
            @reset="reset" 
            @loadData ="getData" 
            @sentSelectedItems="getSelectedItems"
            @sentEditData="getEditData"
            @changePageSize="changePageSize"
            @rankData = "rankData"
            >
        </Tables>
        <Modals 
            :id ="'modal'"
            :title ='title' 
            :field = 'field' 
            :collectField = "collect_field"
            :status = "status" 
            :errorMsg="errorMsg" 
            :type="type" 
            :momalNotice = "'注：目的网段可配置成0.0.0.0/0表示全网段'"
            :selectedItems="selectedItems" 
            :editData = "edit_data"  
            @sentErrorMsg = "getErrorMsg"
            @addEvent ="addData" 
            @editEvent = "editData" 
            @delEvent = "delDada">
        </Modals>
    </div>
</template>
<script>
    import Tables from 'components/common/Tables'
    import Modals from 'components/common/Modals'
    import NetService from 'services/netService' 
    import PageData from 'libs/PageData'

    let butns = [
         {
            type:'add',
            name:'添加',
            event:'addEvent',
            icon:'/static/img/add.png',
            class:'add_btn',
        },
        {
            type:'del',
            name:'删除',
            event:'delEvent',
            icon:'/static/img/delete.png',
            class:'del_btn',
        },
        {
            type:'edit',
            name:'编辑',
            event:'editEvent',
            icon:'/static/img/modpic.png',
            class:'edit_btn',
        }
    ]
    let field = [
        {
            type:'checkbox',
        },
        {
            type:'rank',
        },
        {
            type:'sipinput',
            label:'目的网段',
            name:'ip',          
            regex:/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)(\/(\d|[1-2]\d|3[0-2]))$|^(0\.0\.0\.0\/0)$/,
            requried:true, 
            remark:'*必填',
           
        },
        {
            type:'ipinput',
            label:'网关地址',
            name:'gateway',
            regex:/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/,
            requried:true, 
            remark:'*必填'  
        }
    ]
    export default {
		components:{
		    Tables,
            Modals
	    },
        data() {
            return{
                title:'静态路由',
                butns:butns,
                field:field,
                tableData:[],
                pageSize:[10, 20, 40, 80],
                per_num:25,
                total_num:1,
                cur_page:1,
                size: 10,
                collect_field:'id',
                status:'',
                errorMsg:'',
                type:'',
                selectedItems:[],
                edit_data:{},
                size:25,
                order:'',
                by:''
            }
        },
        created() {
            this.getData()
        },
        methods:{
            getSelectedItems(selectedItems) {
                this.selectedItems = selectedItems
            },
            getErrorMsg(msg) {
                this.errorMsg = msg
            },
            getEditData(editdata) {
                this.edit_data = editdata 
            },
            reset(type) {
                this.status = ""
                this.errorMsg = ''
                this.type = type
                $('.inputs_wrap .error_foramt').removeClass('error_foramt')
            },
            changePageSize (nowpage, size) {
                this.cur_page = nowpage
                this.size = size
                this.getData()
            },
            rankData(param) {
                this.order = param.order
                this.by = param.by
                this.getData()
            },
            getData(){
                NetService.getBindRoute(this.cur_page,this.size,this.order,this.by)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.tableData = res['24'].data
                        this.total_num = res['24'].count
                    }
                })
            }, 
            addData(params){
                let conf = `${params.ip}|${params.gateway}`
                this.status ='ing'
                NetService.addBindRoute(conf)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.status ='ok'
                        this.getData()
                        $('#modal').modal('hide')
                    }else{
                        this.status ='error'
                        this.errorMsg = res.errorMessage
                    }
                })
            }, 
            editData(params){
                let conf = `${params.id}|${params.ip}|${params.gateway}`
                this.status ='ing'
                NetService.editBindRoute(conf)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.status ='ok'
                        this.getData()
                        $('#modal').modal('hide')
                    }else{
                        this.status ='error'
                        this.errorMsg = res.errorMessage
                    }
                })
            },
            delDada(params){
                this.status ='ing'
                NetService.delBindRoute(params.ids)
                .then((res)=>{
                    if(res.errcode === 0){
                        this.status ='ok'
                        this.getData()
                        $('#modal').modal('hide')
                        this.selectedItems = []
                    }else {
                        this.status ='error'
                        this.errorMsg = res.errorMessage	
                    }
                })
            }
        }
    }
</script>